import 'package:flutter/material.dart';
import 'package:znmm/config/app_color.dart';
import 'package:znmm/network/notify_listener.dart';
import 'mag/tool_box_mag.dart';
import 'model/currency_rate_model.dart';
import 'view/tool_top_view.dart';

class ToolBoxPage extends StatefulWidget {
  const ToolBoxPage({super.key});

  @override
  State<ToolBoxPage> createState() => _ToolBoxPageState();
}

class _ToolBoxPageState extends State<ToolBoxPage> {
  int _iNotiId1 = -1;

  @override
  void initState() {
    super.initState();

    //添加监听者
    _iNotiId1 = NotifyCenter().addObserver(NotifyKey.toolBoxKey, (object) {
      if (mounted) {
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    NotifyCenter().removeCallback(_iNotiId1);

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.backgroundColor,
      appBar: AppBar(
        title: const Text('工具箱'),
        centerTitle: true,
        elevation: 0,
      ),
      body: Column(
        children: [
          const ToolTopView(),
          const SizedBox(height: 10),
          // 更多工具
          Expanded(
            child: ListView.separated(
              padding: const EdgeInsets.all(8.0),
              itemCount: ToolBoxMag().rateModelList.length, // 设置列表项的数量
              itemBuilder: (context, index) {
                CurrencyRateModel rateMol = ToolBoxMag().rateModelList[index];
                return Row(
                  children: [
                    const SizedBox(
                      width: 10,
                    ),
                    Text(
                      "${rateMol.currency}(${rateMol.country})", // 币种代码
                      style: const TextStyle(
                        fontSize: 18.0,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const Spacer(),
                    Text(
                      rateMol.rate.toStringAsFixed(4), // 汇率值，保留4位小数
                      style: const TextStyle(
                        fontSize: 16.0,
                      ),
                    ),
                    const SizedBox(
                      width: 10,
                    )
                  ],
                );
              },
              separatorBuilder: (context, index) {
                return const Divider(); // 分隔线
              },
            ),
          ),
        ],
      ),
    );
  }

  // 工具栏网格
  // Widget _buildToolGrid() {
  //   return ListView.builder(
  //     shrinkWrap: true,
  //     physics: const NeverScrollableScrollPhysics(),
  //     itemCount: 5, // 假设有5个工具项
  //     itemBuilder: (context, index) {
  //       return const Icon(Icons.widgets);
  //     },
  //   );
  // }
}
